import { BaseDeployPanel } from "../../../equipment/deploy/panel/BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { ObjCreate } from "../../../common/ObjCreate";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../../../equipment/deploy/DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { MsgManager } from "../../../../manager/MsgManager";
import { ItemsUtil } from "../../../shortcuts/item/ItemsUtil";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../../equipment/data/EquipmentModuleConst";
import { Cache } from "../../../../cache/Cache";
import { PetUtil } from "../../../pet/info/PetUtil";
import { EBind } from "../../../../../../Message/Public/EBind";
import { EquipUpdateParams } from "../../../equipment/data/EquipUpdateParams";
import { PetEquipForgeConfig } from "../../../../resource/PetEquipForgeConfig";
import { ItemData } from "../../../../resource/info/ItemData";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { TItemWeapon } from "../../../../../../Message/Db/Tables/TItemWeapon";
import { EColor } from "../../../../../../Message/Public/EColor";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { _EBindAfterUse } from "../../../../../../EBind/_EBindAfterUse";
type int = number;
//class PetEquipRecastPanel
    
    export  class PetEquipRecastPanel extends BaseDeployPanel
    {
        constructor()
        {
            this.posData = [{"x": 100, "y": 115, "label": Language.getString(61316), "canRemove": true, "noBoarder": true}, {"x": 208, "y": 115, "label": Language.getString(61320), "canRemove": false, "noBoarder": true}, {"x": 156, "y": 245, "label": Language.getString(61321), "canRemove": true, "noBoarder": false}];
            this.buttonLabel = Language.getString(61330);
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc3=null;
            var loc4=null;
            super.createChildren();
            ObjCreate.createLabel(Language.getString(61333), 0, 80, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16445696);
            var loc1=new GImageBitmap("EquipMerge_arrow.swf", 118, 127);
            this.addChild(loc1);
            var loc2=0;
            while (loc2 < this.posData.length) 
            {
                loc3 = this.posData[loc2];
                (loc4 = new DeployPanelItem(loc2, loc3.noBoarder)).posIndex = loc2;
                loc4.x = loc3["x"];
                loc4.y = loc3["y"];
                loc4.label = loc3["label"];
                if (loc3.canRemove) 
                {
                    loc4.mouseEnabled = true;
                    loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc4);
                allItem.push(loc4);
                ++loc2;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 305;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (!this.checkCanRecast()) 
            {
                return;
            }
            var loc2=arg1.target;
            switch (loc2) 
            {
                case confirmButton:
                {
                    if (this.checkIsChangeToBind()) 
                    {
                        loc1 = Language.getString(60147);
                        Alert.show(loc1, Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onStartRecastAlertHandler);
                    }
                    else 
                    {
                        this.onConfirm();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */onStartRecastAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */checkCanRecast(): Boolean
        {
            if (!allItem[0].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60234));
                return false;
            }
            if (this.overLevel()) 
            {
                Alert.show(Language.getString(61362));
                return false;
            }
            if (!allItem[1].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61331));
                return false;
            }
            if (!allItem[1].enabled) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(61325, ItemsUtil.getItemName(allItem[1].itemData)));
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        /* internal  */overLevel(): Boolean
        {
            var loc1=null;
            var loc2=0;
            if (allItem[0].itemData && allItem[2].itemData) 
            {
                loc1 = Cache.instance.pet.getPetInfoByPetEquipPosType(allItem[0].itemData.posType);
                if (loc1 == null) 
                {
                    return false;
                }
                loc2 = PetUtil.getFourDigitLevel(loc1.reinLevel, loc1.level);
                if (loc2 < allItem[2].itemData.itemInfo.item.level) 
                {
                    return true;
                }
            }
            return false;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            if (!allItem[0].itemData || !allItem[0].itemData.itemExInfo || !allItem[1].itemData) 
            {
                return false;
            }
            if (!(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) && allItem[1].itemData.itemInfo.item.bind == EBind._EBindYes) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onConfirm(): void
        {
            var loc1=new EquipUpdateParams();
            loc1.operationId = this._operationId;
            loc1.equipPosType = allItem[0].itemData.posType;
            loc1.equipUid = allItem[0].itemData.uid;
            loc1.materialUid = allItem[1].itemData.uid;
            loc1.itemColor = allItem[0].itemData.color;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc1, false, true));
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            this.showResultEquip();
            if (allItem[0].itemData) 
            {
                loc1 = PetEquipForgeConfig.instance.getCost(2, allItem[0].itemData.color);
            }
            else 
            {
                loc1 = 0;
                if (!this._isResult) 
                {
                    clearAllItem();
                }
            }
            if (this._isResult) 
            {
                confirmButton.enabled = false;
            }
            else 
            {
                confirmButton.enabled = true;
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            if (arg1.category == ECategory._ECategoryPetEquip) 
            {
                this._isResult = false;
                allItem[0].itemData = arg1;
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquip, false, true));
                this.updateStatus();
            }
            return;
        }

        /* internal  */showResultEquip(): void
        {
            var loc6=null;
            var loc7=null;
            var loc8=null;
            var loc9=null;
            var loc10=0;
            if (!allItem[0].itemData) 
            {
                allItem[2].itemData = null;
                return;
            }
            var loc1=allItem[0].itemData;
            var loc2=allItem[0].itemData.itemInfo.item.type;
            var loc3=this.hasBind() ? EBind._EBindYes : _EBindAfterUse;
            var loc4=this.getNewLevel(loc1);
            var loc5=ItemConfig.instance.getAllItemMap();
            var loc11=0;
            var loc12=loc5;
            for(loc7 of loc12) 
            {
                if (!(loc7.item.category == loc1.category && loc7.item.type == loc1.type && loc7.item.bind == loc3 && loc7.item.itemLevel == loc1.itemInfo.item.itemLevel + 1 && loc7.item.level == loc4 && loc7.item.effect == loc1.itemInfo.item.effect)) 
                {
                    continue;
                }
                loc6 = loc7;
                break;
            }
            if (loc6) 
            {
                loc8 = new ItemData(loc6.item.code);
                (loc9 = new Object()).bind = loc3;
                loc9.durable = (loc8.itemInfo.extend as TItemWeapon).durableUpper;
                loc9.prefix = loc1.itemExInfo.prefix;
                loc10 = 1;
                while (loc10 <= 6) 
                {
                    loc9["hole" + loc10] = loc1.itemExInfo["hole" + loc10];
                    ++loc10;
                }
                if (loc8.color != EColor._EColorPurple) 
                {
                    if (loc8.color != EColor._EColorOrange) 
                    {
                        if (loc8.color != EColor._EColorRed) 
                        {
                            if (loc8.color == EColor._EColorRedGold) 
                            {
                                loc9["hole6"] = 1;
                            }
                        }
                        else 
                        {
                            loc9["hole5"] = 1;
                        }
                    }
                    else 
                    {
                        loc9["hole4"] = 1;
                    }
                }
                else 
                {
                    loc9["hole1"] = 1;
                    loc9["hole2"] = 1;
                    loc9["hole3"] = 1;
                }
                loc8.setItemExInfo(loc9);
                allItem[2].itemData = loc8;
            }
            else 
            {
                allItem[2].itemData = null;
            }
            return;
        }

        /* internal  */getNewLevel(arg1: ItemData): int
        {
            if (arg1.itemInfo.item.level < 1000 && arg1.itemInfo.item.itemLevel == 4) 
            {
                return 1001;
            }
            return arg1.itemInfo.item.level;
        }

        /* internal  */hasBind(): Boolean
        {
            if (allItem[0].itemData && allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) 
            {
                return true;
            }
            if (allItem[1].itemData && allItem[1].itemData.itemInfo.item.bind == EBind._EBindYes) 
            {
                return true;
            }
            return false;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < allItem.length && allItem[arg1].itemData) 
            {
                if (arg1 == 2 && !this._isResult) 
                {
                    return;
                }
                this._isResult = false;
                allItem[arg1].itemData = null;
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquip, false, true));
            }
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            if (allItem[0].itemData) 
            {
                allItem[1].itemData = arg1.material;
                allItem[1].amount = arg1.amountNeed;
                allItem[1].enabled = arg1.isEnough;
            }
            this.updateStatus();
            return;
        }

        public /* override */ clear(): void
        {
            this._isResult = false;
            clearAllItem();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            this.clear();
            this._isResult = true;
            allItem[2].itemData = new ItemData(arg3);
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61051));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(61050));
                addUpdateSuccessEffect("equipEffect_qualityUp.swf", 175, 216);
            }
            return;
        }

        /* internal  */get currentEquip(): Array<any>
        {
            var loc1=[];
            if (allItem[0].itemData) 
            {
                loc1.push(allItem[0].itemData);
            }
            return loc1;
        }

        /* internal */ /* var */_operationId: int=16;

        /* internal */ /* var */_isResult: Boolean=false;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_lastPutinIndex: int=0;
    }
